package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.FoodBankDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Food;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = FoodBankService.class)
@Transactional
public class FoodBankServiceImpl implements FoodBankService {
    @Autowired
    private FoodBankDao foodBankDao;


    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//ThreadLocal
        PageHelper.startPage(currentPage,pageSize);//分页插件，会在执行sql之前将分页关键字追加到SQL后面

        Page<Food> page = foodBankDao.findByCondition(queryString);

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Food food) {
        foodBankDao.add(food);
    }

    //批量导入预约设置信息
    public void addAll(List<Food> list) {
        if(list != null && list.size() > 0){
            for (Food food : list) {
                //判断当前日期是否已经进行了设置
                long count = foodBankDao.findCountByOrderDate(food.getName());
                if(count > 0){
                    //如果已经设置，执行更新操作
                    foodBankDao.editNumberByOrderDate(food);
                }else{
                    //如果没有设置，执行插入操作
                    foodBankDao.add(food);
                }
            }
        }
    }

}
